package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.RadioProCityAdapter;
import com.lgqz.himalayafm.bean.RadioProvinceCityEntity;
import com.lgqz.himalayafm.http.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RadioProvinceFragment extends Fragment implements Callback<RadioProvinceCityEntity> {


    private View view;
    private ListView mProCityListview;
    private List<RadioProvinceCityEntity.RadioProvinceCityData> mProCityData;
    private RadioProvinceCityEntity radioProvinceCityData;
    private RadioProCityAdapter adapter;
    private String provinceCode;

    public RadioProvinceFragment() {
        // Required empty public constructor
    }

    public static RadioProvinceFragment newInstance(String provinceCode) {

        Bundle args = new Bundle();
        args.putString("provinceCode", provinceCode);
        RadioProvinceFragment fragment = new RadioProvinceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        provinceCode = bundle.getString("provinceCode");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_radio_viewpage_province, container, false);
        //准备listview的实例
        mProCityListview = ((ListView) view.findViewById(R.id.radio_pro_viewpage_listview));
        //准备数据源
        mProCityData = new ArrayList<>();
        //下载数据
        downloadData();
        //适配器的准备
        adapter = new RadioProCityAdapter(mProCityData,getContext());
        mProCityListview.setAdapter(adapter);
        return view;
    }

    /**
     * 下载数据
     */
    private void downloadData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getProCityStaData("1", "20", provinceCode, "2").enqueue(this);
    }

    /**
     * 下载的主要方法
     *
     * @param call
     * @param response
     */
    @Override
    public void onResponse(Call<RadioProvinceCityEntity> call, Response<RadioProvinceCityEntity> response) {
        radioProvinceCityData = response.body();
        //Log.i("1620", "数据是"+(radioProvinceCityData.getResult()==null));
        mProCityData.addAll(radioProvinceCityData.getResult());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<RadioProvinceCityEntity> call, Throwable t) {
        //Log.i("1620", "错误的路线"+t.getMessage());
    }
}
